const express = require('express');
const router = express.Router();
const db = require('./db');

// 获取所有角色
router.get('/', async (req, res) => {
    try {
        const roles = await db.query('SELECT * FROM Role');
        res.json(roles);
    } catch (err) {
        console.error(err);
        res.status(500).send('Server Error');
    }
});

// 根据角色ID获取角色信息
router.get('/:id', async (req, res) => {
    const roleId = req.params.id;
    try {
        const role = await db.query('SELECT * FROM Role WHERE id = ?', [roleId]);
        if (role.length > 0) {
            res.json(role[0]);
        } else {
            res.status(404).send('Role not found');
        }
    } catch (err) {
        console.error(err);
        res.status(500).send('Server Error');
    }
});

module.exports = router;